package com.ld.linkes_desdemo;

/***************************************
 * 演示如何调用各个API
 ***************************************/

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        view_RES.setText("");				//状态显示栏清空
        setListensers();
    }

    private Button button_desen;			//连接SD卡
    private Button button_desde;		//断开SD卡
    private EditText cmd_DATA;				//APDU指令输入框
    private EditText cmd_KEY;				//外部认证密钥输入框
    private TextView view_RES;				//返回值显示框

    private void findViews(){
        button_desen = (Button)findViewById(R.id.desen);
        button_desde = (Button)findViewById(R.id.desde);
        view_RES = (TextView)findViewById(R.id.result);
        cmd_DATA = (EditText)findViewById(R.id.cmd);
        cmd_KEY = (EditText)findViewById(R.id.Authentext);
    }

    //Listen for button clicks
    private void setListensers(){
        button_desen.setOnClickListener(DESEN);
        button_desde.setOnClickListener(DESDE);
    }

    private Button.OnClickListener DESEN = new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int i = 0,j = 0;
            String DATA = cmd_DATA.getText().toString();
            String masterKey = cmd_KEY.getText().toString();
            i = masterKey.length();
            j = DATA.length();
            Log.i("aaa","desutil:"+DesUtil.encryptDes(DATA));
            if(j != 16){
                view_RES.setText("数据必须为8字节\n");
            }
            else{
                if(i == 16){
                    String DATA1 = TripleDes.encryption(DATA,masterKey);
                    view_RES.setText(DATA1);
                }
                if(i == 32){
                    String DATA2 = TripleDes.DES_3(DATA, masterKey, 0);
                    view_RES.setText(DATA2);
                }
                if(i != 16 && i != 32){
                    view_RES.setText("密钥只能为16或32字节");
                }
            }
        }
    };

    private Button.OnClickListener DESDE = new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int i = 0,j = 0;
            String DATA = cmd_DATA.getText().toString();
            String masterKey = cmd_KEY.getText().toString();
            i = masterKey.length();
            j = DATA.length();
            if(j != 16){
                view_RES.setText("数据必须为8字节\n");
            }
            else{
                if(i == 16){
                    String DATA1 = TripleDes.discryption(DATA,masterKey);
                    view_RES.setText(DATA1);
                }
                if(i == 32){
                    String DATA2 = TripleDes.DES_3(DATA, masterKey, 1);
                    view_RES.setText(DATA2);
                }
                if(i != 16 && i != 32){
                    view_RES.setText("密钥只能为16或32字节");
                }
            }
        }
    };


}
